package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.service.IPayChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "支付参数操作",tags = {"支付参数"},description = "支付参数CRUD操作")
@RestController
public class PayParamController {
    @Reference
    IPayChannelService payChannelService;


    @ApiOperation("获取平台服务类型")
    @GetMapping(value="/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel(){

        return payChannelService.queryPlatformChannel();
    }

    @ApiOperation("根据服务类型编码查询支付渠道列表")
    @GetMapping(value="/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable String platformChannelCode){

        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }

    @RequestMapping(value = "/my/pay-channel-params",method =
            {RequestMethod.POST,RequestMethod.PUT})
    public void savePayParam(@RequestBody PayChannelParamDTO payChannelParamDTO){
            // 非空校验
        if(StringUtils.isEmpty(payChannelParamDTO.getChannelName())){
            throw new BusinessException(CommonErrorCode.E_100101);
        }
        //没有商户ID如何获取
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParamDTO);

    }

    @ApiOperation("根据应用id和服务类型编码查询支付渠道参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryPayChannelParam(@PathVariable String appId,@PathVariable String platformChannel){

        return payChannelService.queryPayChannelParamByAppAndPlatform(appId,platformChannel);
    }

    @ApiOperation("根据应用id和服务类型编码和支付渠道编码查询支付渠道参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,@PathVariable String platformChannel,@PathVariable String payChannel){

        return payChannelService.queryParamByAppPlatformAndPayChannel(appId,platformChannel,payChannel);
    }
}
